package cn.iocoder.yudao.module.member.assets.dal.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.api.member.task.enums.TaskStatusEnum;
import cn.iocoder.yudao.module.member.assets.bo.CryptocurrencyIncreasingTaskCountBO;
import cn.iocoder.yudao.module.member.assets.bo.CryptocurrencyIncreasingTaskSimpleBO;
import cn.iocoder.yudao.module.member.assets.dal.dataobject.CryptocurrencyIncreasingTaskDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 联盟豆增值任务 Mapper
 *
 * @author 山野羡民
 */
@Mapper
public interface CryptocurrencyIncreasingTaskMapper extends CoreMapper<CryptocurrencyIncreasingTaskDO> {

    default List<CryptocurrencyIncreasingTaskSimpleBO> selectAvailableUserIdAndTaskIds() {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(CryptocurrencyIncreasingTaskDO::getId);
        wrapper.select(CryptocurrencyIncreasingTaskDO::getUserId);
        wrapper.gt(CryptocurrencyIncreasingTaskDO::getExpireTime, LocalDateTime.now());
        wrapper.in(CryptocurrencyIncreasingTaskDO::getStatus, List.of(TaskStatusEnum.PENDING.getValue(), TaskStatusEnum.DOING.getValue()));
        wrapper.orderByAsc(CryptocurrencyIncreasingTaskDO::getStatus); // 排序顺序：0-未开始、1-进行中
        wrapper.orderByDesc(CryptocurrencyIncreasingTaskDO::getId); // 最新创建的优先
        List<CryptocurrencyIncreasingTaskDO> list = selectList(wrapper);
        if (CollUtil.isEmpty(list)) {
            return List.of();
        }
        return list.stream().map(it -> {
            CryptocurrencyIncreasingTaskSimpleBO bo = new CryptocurrencyIncreasingTaskSimpleBO();
            bo.setId(it.getId());
            bo.setUserId(it.getUserId());
            return bo;
        }).toList();
    }

    default List<CryptocurrencyIncreasingTaskDO> selectListAvailableByUserId(Long userId) {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(CryptocurrencyIncreasingTaskDO::getUserId, userId);
        wrapper.gt(CryptocurrencyIncreasingTaskDO::getExpireTime, LocalDateTime.now());
        wrapper.in(CryptocurrencyIncreasingTaskDO::getStatus, List.of(TaskStatusEnum.PENDING.getValue(), TaskStatusEnum.DOING.getValue()));
        wrapper.orderByAsc(CryptocurrencyIncreasingTaskDO::getStatus); // 排序顺序：0-未开始、1-进行中
        wrapper.orderByDesc(CryptocurrencyIncreasingTaskDO::getId); // 最新创建的优先
        return selectList(wrapper);
    }

    default List<CryptocurrencyIncreasingTaskDO> selectListByStatusAndLessThanExpireTime(Integer status, LocalDateTime expiredTime) {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(CryptocurrencyIncreasingTaskDO::getStatus, status);
        wrapper.le(CryptocurrencyIncreasingTaskDO::getExpireTime, expiredTime);
        return selectList(wrapper);
    }

    @Select("""
            SELECT `in_amount`, COUNT(1) AS count
            FROM `member_cryptocurrency_increasing_task`
            WHERE `user_id`=#{userId} AND `status`=#{status}
            GROUP BY `in_amount`
            """)
    List<CryptocurrencyIncreasingTaskCountBO> count(@Param("userId") Long userId, @Param("status") Integer status);

}